/// 路由器
import type { RouteRecordRaw } from 'vue-router'
import { createRouter, createWebHistory } from 'vue-router'
import Main from '@/views/Main/Index.vue'

const modules = import.meta.glob('./routes/**/*.ts', { eager: true })
const mainRoutes: RouteRecordRaw[] = []

// 加入到路由集合中
Object.keys(modules).forEach((key) => {
  // console.log(modules[key].routes)
  const mod = (modules as Entity)[key].default || {}
  const modList = Array.isArray(mod) ? [...mod] : [mod]
  mainRoutes.push(...modList)
})

/** 路由集合 */
const routes = [
  {
    name: 'login',
    path: '/login',
    component: () => import('@/views/Login/Index.vue'),
  },
  {
    name: 'main',
    path: '/',
    component: Main,
    children: mainRoutes,
  },
  {
    path: '',
    redirect: '/demo/crud',
  },
  {
    path: '/redirect',
    component: Main,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/Redirect/Index.vue'),
      },
    ],
  },
] as RouteRecordRaw[]

/** 路由器 */
const router = createRouter({
  history: createWebHistory(),
  routes,
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

export default router
